#include<string.h>
#include<stdlib.h>
#include<stdio.h>
#include "../include/llist.h"
void print_list(l_node *head);


l_node *list_insert_head( l_node *head, void *data )
{
    l_node *new_node;
    new_node=(l_node *)malloc(sizeof(l_node));
    new_node->data=data;
    new_node->next=head;
    return new_node;
}

l_node *list_insert_sortorder( l_node *head,void *data,char *filename)
{
    l_node *cur_node,*new_node,*pre_node;
    h_node *tra_data;
    cur_node=head;
    new_node=(l_node *)malloc(sizeof(l_node));
    new_node->data=data;
    if(head==NULL)
    {
        new_node->next=NULL;
        return new_node;
    }
    tra_data=(h_node*)cur_node->data;
    if(strcmp(tra_data->file_name,filename)>=0)
    {
        new_node->next=head;
        return new_node;
    }
    pre_node=cur_node;
    cur_node=cur_node->next;
    while(cur_node!=NULL)
    {
        tra_data=(h_node *)cur_node->data;
        if(strcmp(tra_data->file_name,filename)>=0 )
        {
            new_node->next=cur_node;
            pre_node->next=new_node;
            return head;
        }
        pre_node=cur_node;
        cur_node=cur_node->next;
    }
    new_node->next=NULL;
    pre_node->next=new_node;
    return head;
}


l_node *list_delete_head( l_node *head )
{
    l_node *next_node;
    if(head==NULL)
        return NULL;
    next_node=head->next;
    free(head);
    return next_node;
}

l_node *list_delete_node( l_node *head, void *data )
{
    l_node *pre_node,*cur_node;
    if(head==NULL)
        return NULL;
    pre_node=head;
    cur_node=head->next;
    if(strcmp(((h_node *)head->data)->file_name,((h_node *)data)->file_name)==0 && strcmp(((h_node *)head->data)->path,((h_node *)data)->path)==0)
    {
        free(head);
        return cur_node;
    }
    while(cur_node!=NULL)
    {
        if(strcmp(((h_node *)cur_node->data)->file_name,((h_node *)data)->file_name)==0 && strcmp(((h_node *)cur_node->data)->path,((h_node *)data)->path)==0)
        {
            pre_node->next=cur_node->next;
            free(cur_node);
            return head;
        }
        pre_node=cur_node;
        cur_node=cur_node->next;
    }
    return head;
}

int list_get_data_list( l_node *head , char *filename,h_node **data[])
{
    int count=0;
    h_node *tra_data;
    l_node *cur_node;
    cur_node=head;
    while(cur_node!=NULL)
    {
        tra_data=(h_node *)cur_node->data;
        if(strcmp(tra_data->file_name,filename)==0)
        {
            count++;
        }
        cur_node=cur_node->next;
    }
    *data=(h_node **)malloc(count * sizeof(h_node *));
    count=0;
    cur_node=head;
    while(cur_node!=NULL)
    {
        tra_data=(h_node *)cur_node->data;
        if(strcmp(tra_data->file_name,filename)==0)
        {
            (*data)[count++]=tra_data;
        }
        cur_node=cur_node->next;
    }
    return count;
}


int list_length(l_node *head)
{
    l_node *temp=head;
    int count=0;
    while(temp!=NULL)
    {
         count++;
         temp=temp->next;
    }
    return count;
}


void print_list(l_node *head)
{
    l_node *cur_node;

    int *a;
    cur_node=head;
    while(cur_node!=NULL)
    {

        a=(int *)cur_node->data;
        printf("%d\t",*a);
        cur_node=cur_node->next;
    }

    printf("\n");
}
